package hello.javaConfig;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.data.mapping.model.IllegalMappingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/8/23.
 */
@ControllerAdvice
@Controller
public class GlobalException implements ErrorController{
    /** 页面未找到404异常。spring boot默认的error视图配置.只需要实现这个映射即可 */
    @RequestMapping(value = "/error")
    public String errorResolver(Model model) {
        model.addAttribute("exception",new Exception("有问题哦,没有这个页面的说"));
        return "view";
    }
    /** 方法 请求异常.如请求的方式不正确.POST用GET请求方式等等 */
    @ExceptionHandler(value = {Exception.class, IllegalMappingException.class})
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", request.getRequestURL());
        mav.setViewName("view");
        return mav;
    }

    @Override
    public String getErrorPath() {
        return "error";
    }


}
